// pages/my/turnover/turnover.js
import moment from '../../../utils/moment.js'
var app = getApp()
Page({
  data: {
    showturnover: "#539FFF",
    showservice: "#666666",
    showprofit: "#666666",
    year: new Date().getFullYear()+"",      // 年份
    month: new Date().getMonth() + 1<10?"0"+(new Date().getMonth()+1):new Date().getMonth() + 1+'',    // 月份
    day: new Date().getDate()+"",
    header_show:false,
    prev: false,
    next: false,
    allConfirm:false,
    calendarShow:false,
    turnoversList: [],
    thisList:null,
    days_style:[],
    accountId:'',
    cash:'',
    cashin:'',
    isProfit:false,
    type:0,
  },
  onLoad: function(options) {
    let type = options.type //0新商品  //1旧商品
    this.setData({
      type
    })
    app.getMemInfo().then(res=>{
      let accountId = res.data.id
      this.setData({
        accountId
      })
      this.getTime(accountId)
    })
    this.dayChoose()
  },
  showturnover: function(e) {
    this.setData({
      showturnover: "#539FFF",
      showservice: "#666666",
      showprofit: "#666666",
      isProfit:false
    })
  },
  // showservice: function(e) {
  //   this.setData({
  //     showturnover: "#666666",
  //     showservice: "#539FFF",
  //     showprofit: "#666666",
  //     isProfit:true
  //   })
  // },
  showprofit: function(e) {
    this.setData({
      showturnover: "#666666",
      showservice: "#666666",
      showprofit: "#539FFF",
      isProfit:true
    })
  },
  //calendar
  //点击日历事件
  dayClick(e) {
    console.log(e)
    this.setData({
      allConfirm:false,
      year:e.detail.year+'',
      month:e.detail.month<10?'0'+e.detail.month:''+e.detail.month,
      day:e.detail.day+''
    })
    this.dayChoose()
  },
  //日期选择
  dayChoose(){
    let days_style=[]
    days_style.push({
      month:'current',
      day:this.data.day,
      color:'#fff',
      background:'#539FFF'
    })
    this.setData({
      days_style
    })
  },
  //点击标题右侧日期打开日历
  bindTimeCheck(){
    this.setData({
      calendarShow:true
    })
  },
  //日历取消按钮事件
  bindCancel(e){
    this.setData({
      calendarShow:false
    })
  },
  //日历确定按钮事件
  bindConfirm(){
    this.setData({
      calendarShow:false
    })
    this.getTime(this.data.accountId)
  },
  //日历全部按钮事件
  allConfirm(e){
    let days_style = []
    this.setData({
      allConfirm:true,
      days_style
    })
  },
  next(e){
    console.log(e.detail.currentMonth)
    this.setData({
      year:e.detail.currentYear+'',
      month:e.detail.currentMonth<10?'0'+e.detail.currentMonth:''+e.detail.currentMonth
    })
  },
  prev(e){
    this.setData({
      year:e.detail.currentYear+'',
      month:e.detail.currentMonth<10?'0'+e.detail.currentMonth:''+e.detail.currentMonth
    })
  },
  getTime(accountId){
    let startTime=''
    let endTime=''
    let data = {}
    
    if (this.data.allConfirm) {
      startTime =  this.data.year+'-'+this.data.month+'-01'
      endTime = moment(startTime).endOf('month').format('YYYY-MM-DD')

      data = {
        accountId,
        startTime,
        endTime
      }
      if (this.data.type==0) {
        this.getCashierList(data)
      }else{
        this.getOld(data)
      }
    }else{
      startTime = this.data.year+'-'+this.data.month+'-'+this.data.day
      endTime = startTime
      data = {
        accountId,
        startTime,
        endTime
      }
      if (this.data.type==0) {
        this.getCashierList(data)
      }else{
        this.getOld(data)
      }
    }
  },
  getCashierList(data){
    var that = this
    console.log(data)
    app.request('wx_app/agent/cashier/list',data,'GET').then(res=>{
      console.log(res)
      let turnoversList = []
      res.data.flows.forEach(item=>{
        turnoversList.push({
          orderId:item.orderId,
          name:item.flowType,
          time:item.amountDate,
          memName:item.accountName,
          role:item.accountRole,
          price:item.amount,
          payWay:item.amountType,
          profit:item.profit,
          actualTurnover:item.actualTurnover,
          isOldForNew:item.isOldForNew
        })
      })
      that.setData({
        turnoversList,
        cash:res.data.cash.cash,
        cashin:res.data.cash.cash_in
      })
    })
  },
  bindToDetai(e){
    console.log(e.currentTarget.dataset)
    let id = e.currentTarget.dataset.order
    let name = e.currentTarget.dataset.name
    let type = e.currentTarget.dataset.type
    if (this.data.type==0) {
      // wx.navigateTo({
      //   url:'../orderDetail/orderDetail?orderId='+id+'&title=客户订单详情&type=0&pay=1&saler='+name
      // })
      if (name==0) {
        wx.navigateTo({
          url:'../orderDetail/orderDetail?orderId='+id+'&title=客户订单详情&type=0&pay=1&saler='+name
        })
      }else{
        wx.navigateTo({
          url:'../replaceProduct/replacePay?orderId='+id+'&type=2'
        })
      }
    }else{
      if (type=='出售旧商品') {
        wx.navigateTo({
          url:'../orderDetail/orderDetail?orderId='+id+'&title=客户订单详情&type=0&pay=1&saler='+name
        })
      }else{
        wx.navigateTo({
          url:'../replaceProduct/replacePay?orderId='+id+'&type=2'
        })
      }
    }
  },
  getOld(data){
    console.log(data)
    let that = this
    app.request('wx_app/oldForNew/cashier',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let turnoversList = []
        res.data.detaileds.forEach(item=>{
          turnoversList.push({
            orderId:item.orderId,
            name:item.type,
            time:item.createTime,
            memName:item.accpuntName,
            role:item.role,
            price:item.turnover,
            payWay:item.payWay,
            profit:item.profit,
            actualTurnover:item.actualTurnover,
          })
        })
        that.setData({
          cash:res.data.cash,
          cashin:res.data.cash_in,
          turnoversList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})


